package tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class binaryTreePaths {
    List<String> result = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null){
            return result;
        }
        List<Integer> paths = new ArrayList<>();
        getPath(root, paths);
        return result;
    }
    public void getPath(TreeNode root, List<Integer> record){
        if (root == null){
            return;
        }
        record.add(root.val);
        getPath(root.left, record);
        getPath(root.right, record);
        if (root.left == null && root.right == null){
            StringBuilder path = new StringBuilder();
            for (int i = 0; i < record.size()-1; i++) {
                path.append(record.get(i)).append("->");
            }
            path.append(record.get(record.size()-1));
            result.add(path.toString());
            record.remove(record.size()-1);
        } else {
            record.remove(record.size()-1);
        }
    }
}
